// 多语言支持配置
const translations = {
    zh: {
        nav: {
            home: "首页",
            about: "关于我",
            skills: "技能",
            projects: "项目",
            contact: "联系我",
            blog: "博客"
        },
        hero: {
            title: "Python Developer",
            subtitle: "专注于消息中间件、实时数据处理和高可用架构设计",
            cta: {
                primary: "查看我的作品",
                secondary: "联系我"
            }
        },
        about: {
            title: "关于我",
            intro: "我是一名Python开发者，专注于消息中间件、实时数据处理和高可用架构设计。",
            description: "在技术领域，我热衷于构建高性能、可扩展的系统架构。我的专业方向包括消息队列系统、实时数据流处理和分布式系统设计。",
            hobbies: "除了技术之外，我喜欢Rust语言，热爱动漫文化，包括《幸运星》、《孤独摇滚》和《摇曳露营》等作品。",
            tech_stack: "技术栈",
            expert: "专家级",
            proficient: "熟练级",
            learning: "学习级",
            experience: "专业经历",
            achievements: "个人成就",
            graduation: "清华大学优秀毕业生",
            mvp: "微软MVP",
            certification: "华为开发者认证"
        },
        skills: {
            title: "技能",
            programming: "编程语言",
            web: "Web开发",
            database: "数据库",
            tools: "开发工具"
        },
        projects: {
            title: "项目展示",
            filter: {
                all: "全部",
                web: "Web应用",
                mobile: "移动应用",
                ai: "AI项目"
            },
            view: "查看项目",
            source: "源代码"
        },
        contact: {
            title: "联系我",
            info: "联系信息",
            subtitle: "有任何问题或合作意向，欢迎随时联系我！",
            form: "发送消息",
            name: "姓名",
            email: "邮箱",
            subject: "主题",
            message: "留言",
            send: "发送消息"
        },
        social: {
            personal_site: "个人网站",
            email: "邮箱联系"
        },
        lang: {
            zh: "中文",
            en: "EN"
        },
        footer: {
            copyright: "© 2024 我的作品集. 保留所有权利.",
            design: "用心设计与开发"
        },
        theme: {
            dark: "切换到深色模式",
            light: "切换到浅色模式",
            switched_dark: "已切换到深色模式",
            switched_light: "已切换到浅色模式"
        }
    },
    en: {
        nav: {
            home: "Home",
            about: "About",
            skills: "Skills",
            projects: "Projects",
            contact: "Contact",
            blog: "Blog"
        },
        hero: {
            title: "Python Developer",
            subtitle: "Specializing in message middleware, real-time data processing, and high-availability architecture",
            cta: {
                primary: "View My Work",
                secondary: "Contact Me"
            }
        },
        about: {
            title: "About Me",
            intro: "I am a Python developer specializing in message middleware, real-time data processing, and high-availability architecture.",
            description: "In the technology field, I am passionate about building high-performance, scalable system architectures. My expertise includes message queue systems, real-time data stream processing, and distributed system design.",
            hobbies: "Beyond technology, I enjoy Rust language and love anime culture, including works like Lucky Star, Bocchi the Rock, and Laid-Back Camp.",
            tech_stack: "Technology Stack",
            expert: "Expert",
            proficient: "Proficient",
            learning: "Learning",
            experience: "Experience",
            achievements: "Achievements",
            graduation: "Outstanding Graduate of Tsinghua University",
            mvp: "Microsoft MVP",
            certification: "Huawei Developer Certification"
        },
        skills: {
            title: "Skills",
            programming: "Programming Languages",
            web: "Web Development",
            database: "Database",
            tools: "Development Tools"
        },
        projects: {
            title: "Projects",
            filter: {
                all: "All",
                web: "Web Apps",
                mobile: "Mobile Apps",
                ai: "AI Projects"
            },
            view: "View Project",
            source: "Source Code"
        },
        contact: {
            title: "Contact Me",
            info: "Contact Information",
            subtitle: "If you have any questions or collaboration ideas, feel free to contact me!",
            form: "Send Message",
            name: "Name",
            email: "Email",
            subject: "Subject",
            message: "Message",
            send: "Send Message"
        },
        social: {
            personal_site: "Personal Website",
            email: "Email Contact"
        },
        lang: {
            zh: "中文",
            en: "EN"
        },
        footer: {
            copyright: "© 2024 My Portfolio. All rights reserved.",
            design: "Crafted with care and passion"
        },
        theme: {
            dark: "Switch to Dark Mode",
            light: "Switch to Light Mode",
            switched_dark: "Switched to dark mode",
            switched_light: "Switched to light mode"
        }
    }
};

// 翻译函数
function translate(key, lang = currentLang) {
    const keys = key.split('.');
    let result = translations[lang];
    
    for (const k of keys) {
        if (result && typeof result === 'object' && result[k] !== undefined) {
            result = result[k];
        } else {
            return key; // 如果找不到翻译，返回原始键
        }
    }
    
    return result;
}

// 更新页面文本
function updatePageText(lang) {
    // 更新所有带有 data-i18n 属性的元素
    const elements = document.querySelectorAll('[data-i18n]');
    elements.forEach(element => {
        const key = element.getAttribute('data-i18n');
        const translation = translate(key, lang);
        
        // 根据元素类型更新内容
        if (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA') {
            element.placeholder = translation;
        } else {
            element.textContent = translation;
        }
    });
    
    // 更新HTML的lang属性
    document.documentElement.lang = lang;
    
    // 更新页面标题
    const titleElement = document.querySelector('title[data-i18n]');
    if (titleElement) {
        document.title = translate('hero.title', lang);
    }
}

// 切换语言
function switchLanguage(lang) {
    if (translations[lang]) {
        currentLang = lang;
        localStorage.setItem('language', lang);
        updatePageText(lang);
        updateLanguageButtons(lang);
    }
}

// 更新语言按钮状态
function updateLanguageButtons(activeLang) {
    const langButtons = document.querySelectorAll('.lang-btn');
    langButtons.forEach(button => {
        if (button.getAttribute('data-lang') === activeLang) {
            button.classList.add('active');
        } else {
            button.classList.remove('active');
        }
    });
}

// 检测浏览器语言
function detectBrowserLanguage() {
    const browserLang = navigator.language || navigator.userLanguage;
    if (browserLang.startsWith('zh')) {
        return 'zh';
    } else if (browserLang.startsWith('en')) {
        return 'en';
    }
    return 'zh'; // 默认中文
}

// 初始化语言
function initLanguage() {
    // 优先使用本地存储的语言设置
    const savedLang = localStorage.getItem('language');
    if (savedLang && translations[savedLang]) {
        currentLang = savedLang;
    } else {
        // 其次检测浏览器语言
        currentLang = detectBrowserLanguage();
    }
    
    updatePageText(currentLang);
    updateLanguageButtons(currentLang);
}

// 全局变量
let currentLang = 'zh';

// 初始化
document.addEventListener('DOMContentLoaded', function() {
    initLanguage();
    
    // 绑定语言切换按钮事件
    const langButtons = document.querySelectorAll('.lang-btn');
    langButtons.forEach(button => {
        button.addEventListener('click', function() {
            const lang = this.getAttribute('data-lang');
            switchLanguage(lang);
        });
    });
    
    // 监听语言切换事件（供其他模块使用）
    window.addEventListener('languageChanged', function(e) {
        console.log('Language changed to:', e.detail.language);
        // 这里可以添加其他需要响应语言变化的逻辑
    });
});

// 暴露全局函数供其他脚本使用
window.i18n = {
    translate: translate,
    switchLanguage: switchLanguage,
    getCurrentLanguage: function() { return currentLang; }
};